//
//  WNLockApi.swift
//  WNLockDemo
//
//  Created by 裴威 on 01/09/2018.
//  Copyright © 2018 wunu. All rights reserved.
//

import Foundation
import Moya

/// 锁相关操作
///
/// - bindLock: 管理员绑定新锁
/// - activeLock: 管理员激活新锁，启用蓝牙解锁
/// - deleteLock: 管理员删除门锁
/// - findLock: 门锁状态查询
/// - lockList: 门锁列表查询
/// - lockBasecode: 开锁basecode获取
/// - openLock: 开锁行为记录 1成功 -1失败 -2蓝牙连接失败 10网关开锁
/// - addLock: 锁成员申请
/// - grantMember: 锁成员申请信息查询
/// - grantMember_m: 锁成员申请审核
/// - genTempKey: 临时秘钥生成
/// - sendTempKey: 临时秘钥发送
/// - openTempLock: 临时秘钥开锁记录
/// - lockMember: 锁成员列表
/// - changeLockMember: 成员生命周期管理
/// - changeTempKey: 临时秘钥生命周期管理
/// - lockEvent: 开锁记录列表
/// - wxQrCode: 锁二维码
/// - lockManager: 获取锁管理员id
/// - unbindLock: 停用蓝牙解锁功能
/// - resetLock: 更新蓝牙解锁basecode
/// - myLockPwd: 获取个人密码开锁(网关)
/// - setMyPwd: 申请个人密码开锁
/// - getTempKeyInfo: 获取临时秘钥详情
enum WNLockApi {
    case bindLock(parameters: [String: Any])
    case activeLock(lockId: Int)
    case deleteLock(lockId: Int)
    case findLock(lockId: Int?, lockMac: String?)
    case lockList
    case lockBasecode(lockId: Int?, tempKeyId: Int?)
    case openLock(lockId: Int, event: Int)
    case addLock(lockId: Int, requestMsg: String)
    case grantMember(memberId: Int)
    case grantMember_m(memberId: Int, group: Int, state: Int)
    case genTempKey(lockId: Int)
    case sendTempKey(parameters: [String: Any])
    case openTempLock(tempKeyId: Int, event: Int)
    case lockMember(lockId: Int)
    case changeLockMember(memberId: Int, state: Int, isDelete: Bool)
    case changeTempKey(memberId: Int, state: Int, isDelete: Bool)
    case lockEvent(lockId: Int, group: Int, pageIndex: Int, pageSize: Int)
    case wxQrCode(lockId: Int)
    case lockManager(lockId: Int)
    case unbindLock(lockId: Int)
    case resetLock(lockId: Int, baseCode: Int)
    case myLockPwd(lockId: Int)
    case setMyPwd(lockId: Int)
    case deletUserPwd(lockId: Int)
    case getTempKeyInfo(tempkeyId: Int)
}

extension WNLockApi: FxTargetType {
    var path: String {
        switch self {
        case .bindLock:         return "/weapp/bindLock"
        case .activeLock:       return "/weapp/activeLock"
        case .deleteLock:       return "/weapp/deleteLock"
        case .findLock:         return "/weapp/findLock"
        case .lockList:         return "/weapp/lockList"
        case .lockBasecode:     return "/weapp/lockBasecode"
        case .openLock:         return "/weapp/openLock"
        case .addLock:          return "/weapp/addLock"
        case .grantMember:      return "/weapp/grantMember"
        case .grantMember_m:    return "/weapp/grantMember"
        case .genTempKey:       return "/weapp/genTempKey"
        case .sendTempKey:      return "/weapp/genTempKey"
        case .openTempLock:     return "/weapp/openTempLock"
        case .lockMember:       return "/weapp/lockMember"
        case .changeLockMember: return "/weapp/changeLockMember"
        case .changeTempKey:    return "/weapp/changeTempKey"
        case .lockEvent:        return "/weapp/lockEvent"
        case .wxQrCode:         return "/weapp/wxQrCode"
        case .lockManager:      return "/weapp/lockManager"
        case .unbindLock:       return "/weapp/unbindLock"
        case .resetLock:        return "/weapp/resetLock"
        case .myLockPwd:        return "/weapp/myLockPwd"
        case .setMyPwd:         return "/weapp/setMyPwd"
        case .deletUserPwd:     return "/weapp/deletUserPwd"
        case .getTempKeyInfo:   return "/weapp/tempKey"
        }
    }
    var method: Moya.Method {
        switch self {
        case .bindLock,
             .addLock,
             .grantMember_m,
             .sendTempKey,
             .changeLockMember,
             .changeTempKey,
             .resetLock,
             .deletUserPwd:
            return .post
        case .activeLock, .deleteLock, .findLock,
             .lockList, .lockBasecode, .openLock,
             .grantMember, .genTempKey, .openTempLock,
             .lockMember, .lockEvent, .wxQrCode,
             .lockManager, .unbindLock, .myLockPwd,
             .setMyPwd, .getTempKeyInfo:
            return .get
        }
    }
    var task: Task {
        switch self {
        case let .bindLock(parameters), let .sendTempKey(parameters):
            return .requestParameters(parameters: parameters, encoding: URLEncoding.default)
        case let .activeLock(lockId),
             let .deleteLock(lockId),
             let .genTempKey(lockId),
             let .lockMember(lockId),
             let .wxQrCode(lockId),
             let .lockManager(lockId),
             let .unbindLock(lockId),
             let .myLockPwd(lockId),
             let .setMyPwd(lockId),
             let .deletUserPwd(lockId):
            return .requestParameters(parameters: ["lockId": lockId], encoding: URLEncoding.default)
        case .lockList:
            return .requestPlain
        case let .findLock(lockId, lockMac):
            var params: [String: Any] = [:]
            if let lid = lockId {
                params["lockId"] = lid
            }
            if let mac = lockMac {
                params["lockMac"] = mac
            }
            return .requestParameters(parameters: params, encoding: URLEncoding.default)
        case let .lockBasecode(lockId, tempKeyId):
            var params: [String: Any] = [:]
            if let lid = lockId {
                params["lockId"] = lid
            }
            if let tid = tempKeyId {
                params["tempKeyId"] = tid
            }
            return .requestParameters(parameters: params, encoding: URLEncoding.default)
        case let .openLock(lockId, event):
            return .requestParameters(parameters: ["lockId": lockId, "event": event], encoding: URLEncoding.default)
        case let .addLock(lockId, requestMsg):
            return .requestParameters(parameters: ["lockId": lockId, "request": requestMsg], encoding: URLEncoding.default)
        case let .grantMember(memberId):
            return .requestParameters(parameters: ["memberId": memberId], encoding: URLEncoding.default)
        case let .grantMember_m(memberId, group, state):
            return .requestParameters(parameters: ["memberId": memberId, "group": group, "state": state], encoding: URLEncoding.default)
        case let .openTempLock(tempKeyId, event):
            return .requestParameters(parameters: ["tempKeyId": tempKeyId, "event": event], encoding: URLEncoding.default)
        case let .changeTempKey(memberId, state, isDelete),
             let .changeLockMember(memberId, state, isDelete):
            return .requestParameters(parameters: ["memberId": memberId, "state": state, "isDelete": isDelete], encoding: URLEncoding.default)
        case let .lockEvent(lockId, group, pageIndex, pageSize):
            return .requestParameters(parameters: ["lockId": lockId, "group": group, "pageIndex": pageIndex, "pageSize": pageSize], encoding: URLEncoding.default)
        case let .getTempKeyInfo(tempkeyId):
            return .requestParameters(parameters: ["tempKeyId": tempkeyId], encoding: URLEncoding.default)
        case let .resetLock(lockId, baseCode):
            return .requestParameters(parameters: ["lockId": lockId, "baseCode": baseCode], encoding: URLEncoding.default)
        }
    }
}
